package com.doudou.oauth.validate;

import com.doudou.commons.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 傻男人
 * @Date: 2020/5/21 16:07
 * @Version: 1.0
 * @Description:
 */
@Slf4j
@RestController
@Api(value = "生成验证码",tags = "生成验证码")
public class ValidateCodeController{

    @Autowired
    ValidateCodeProcessorHolder validateCodeProcessorHolder;

    @GetMapping("/code/image")
    @ApiOperation(value = "图形验证码",notes = "图形验证码")
    public void createCode(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        validateCodeProcessorHolder.findValidateCodeProcessor("image").create(new ServletWebRequest(request, response));
    }

    @GetMapping("/code/sms")
    @ApiImplicitParam(name = "phone",value="手机号码",dataType="String",required = true)
    @ApiOperation(value = "获取手机验证码",notes = "获取手机验证码")
    public Result createSmsCode(HttpServletRequest request, HttpServletResponse response,
                                @RequestParam("mobile")String phone) throws Exception {
        validateCodeProcessorHolder.findValidateCodeProcessor("sms").create(new ServletWebRequest(request, response));
        return Result.ok("发送成功");
    }
}
